package cate.game.role.res.racebadge;

import cate.common.table.d.GDFunc;
import cate.common.table.res.racebadge.row.RaceBadgeBaseRow;
import cate.common.util.GameResult;
import cate.game.attr.FightAttr;
import cate.game.res.CostSelect;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.res.racebadge.msg.RaceBadgeDataResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class RaceBadges extends FuncOpenChecker {
    @NoteField(value = "族徽列表")
    public List<RaceBadge> list;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if(list == null){
            list = new ArrayList<>();
        }
        checkList();
        list.forEach(a -> a.initialize(role));
    }

    @Override
    @JsonIgnore
    public int getFuncId() {
        return GDFunc.RACE_BADGE;
    }

    @Override
    public void onAfterOpen() {
        checkList();
        noticeUpdate();
    }

    private void checkList(){
        if(isOpen()){
            List<RaceBadgeBaseRow> rows = role.getGame().table.raceBadge.base.list;
            if(CollectionUtils.isNotEmpty(rows) && rows.size() != list.size()){
                Set<Integer> set = list.stream().map(v -> v.type).collect(Collectors.toSet());
                RaceBadge raceBadge;
                for(RaceBadgeBaseRow row : rows){
                    if(!set.contains(row.id)){
                        raceBadge = new RaceBadge(role, row.id);
                        list.add(raceBadge);
                    }
                }
            }
        }
    }

    @JsonIgnore
    private RaceBadge getItem(int type){
        return list.stream().filter(e -> e.type == type).findAny().orElse(null);
    }

    public void levelUp(int type){
        RaceBadge item = getItem(type);
        if(item == null){
            role.getGame().notice.message(role,"错误的族徽类型");
            return;
        }
        GameResult<Void> r = item.levelUp();
        if(!r.ok()){
            role.getGame().notice.message(role, r.message);
            return;
        }
        role.getHistory().action.RaceBadgeLevelUpTime();
        role.getHistory().action.raceBadgeCampLevel(item.type, item.level);
        role.getBase().updatePower();
    }

    public void gradeUp(int type, Map<Integer, CostSelect> csMap) {
        RaceBadge item = getItem(type);
        if(item == null){
            role.getGame().notice.message(role,"错误的族徽类型");
            return;
        }
        GameResult<Void> r = item.gradeUp(csMap);
        if(!r.ok()){
            role.getGame().notice.message(role, r.message);
            return;
        }
        role.getBase().updatePower();
        role.getHistory().action.raceBadgeCampGrade(item.type, item.grade);
    }

    public void noticeUpdate(){
        if(isOpen()){
            role.sendNow(new RaceBadgeDataResp(this));
        }
    }

    public void gmSet(int type, int level){
        RaceBadge item = getItem(type);
        if(item == null){
            role.getGame().notice.message(role,"错误的族徽类型");
            return;
        }
        item.level = level;
        item.times = 0;
        noticeUpdate();
    }

    @JsonIgnore
    public FightAttr getCampAttr(byte camp){
        FightAttr attr = new FightAttr();
        list.stream().filter(e -> e.getRow().camp == camp).findAny().ifPresent(raceBadge -> attr.add(raceBadge.getAttr()));
        return attr;
    }
}
